package com.wilson.integration.lease.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.wilson.integration.lease.common.entity.*;
import com.wilson.integration.lease.common.entity.vo.agreement.AgreementDetailVO;
import com.wilson.integration.lease.common.entity.vo.agreement.AgreementItemVO;
import com.wilson.integration.lease.common.entity.vo.graph.GraphVO;
import com.wilson.integration.lease.common.enums.ItemType;
import com.wilson.integration.lease.common.mapper.*;
import com.wilson.integration.lease.app.service.LeaseAgreementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wilson
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement> implements LeaseAgreementService {

    @Resource
    private LeaseAgreementMapper leaseAgreementMapper;

    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;

    @Resource
    private RoomInfoMapper roomInfoMapper;

    @Resource
    private GraphInfoMapper graphInfoMapper;

    @Resource
    private PaymentTypeMapper paymentTypeMapper;

    @Resource
    private LeaseTermMapper leaseTermMapper;

    @Override
    public List<AgreementItemVO> listItemByPhone(String phone) {
        return leaseAgreementMapper.listItemByPhone(phone);
    }

    @Override
    public AgreementDetailVO getDetailById(Long id) {
        // 1.查询租约信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        if (ObjectUtil.isNull(leaseAgreement)) {
            return null;
        }
        // 2.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId());

        // 3.查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());

        // 4.查询图片信息
        List<GraphVO> roomGraphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, leaseAgreement.getRoomId());
        List<GraphVO> apartmentGraphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, leaseAgreement.getApartmentId());

        // 5.查询支付方式
        PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());

        // 6.查询租期
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());

        AgreementDetailVO agreementDetailVO = new AgreementDetailVO();
        BeanUtils.copyProperties(leaseAgreement, agreementDetailVO);
        agreementDetailVO.setApartmentName(apartmentInfo.getName());
        agreementDetailVO.setRoomNumber(roomInfo.getRoomNumber());
        agreementDetailVO.setApartmentGraphVOList(apartmentGraphVoList);
        agreementDetailVO.setRoomGraphVOList(roomGraphVoList);
        agreementDetailVO.setPaymentTypeName(paymentType.getName());
        agreementDetailVO.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        agreementDetailVO.setLeaseTermUnit(leaseTerm.getUnit());

        return agreementDetailVO;
    }
}




